/*

    Licensed under the Apache License, Version 2.0 (the "License"); 
    you may not use this file except in compliance with the License. 
    You may obtain a copy of the License at 
    http://www.apache.org/licenses/LICENSE-2.0 
    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the License.
      
 */

package jforex.impl;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

/**
 * 
 * Playing audio files
 * 
 */
class AudioPlayer {

    public AudioPlayer() {

    }

    private static final int EXTERNAL_BUFFER_SIZE = 128000;

    public static void main(String[] args) {
        AudioPlayer player = new AudioPlayer();
        player.play("test.mp3");
    }

    public boolean play(String strFilename) {

        boolean rc = false;

        //File soundFile = new File("c:\\temp\\voy-comp-01.wav");
        // String strFilename = args[0];

        File soundFile = new File(strFilename);
        if (soundFile.exists() && soundFile.isFile()) {

            // Set Volume
            if (soundFile.exists()) {
                AudioInputStream audioInputStream = null;
                try {
                    audioInputStream = AudioSystem.getAudioInputStream(soundFile);
                } catch (Exception e) {

                    e.printStackTrace();
                    // System.exit(1);
                }
                if (audioInputStream == null) {
                    return rc;
                }

                AudioFormat audioFormat = audioInputStream.getFormat();

                SourceDataLine line = null;
                DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
                try {
                    line = (SourceDataLine) AudioSystem.getLine(info);

                    line.open(audioFormat);

                    line.getControls();

                } catch (LineUnavailableException e) {
                    e.printStackTrace();
                    // System.exit(1);
                } catch (Exception e) {
                    e.printStackTrace();
                    // System.exit(1);
                }

                line.start();

                int nBytesRead = 0;
                byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
                while (nBytesRead != -1) {
                    try {
                        nBytesRead = audioInputStream.read(abData, 0, abData.length);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if (nBytesRead >= 0) {
                        int nBytesWritten = line.write(abData, 0, nBytesRead);
                    }
                }

                line.drain();

                line.close();
                rc = true;
            }
        }

        return rc;

        // System.exit(0);
    }

}
